﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TrangestAdmin
{
    public partial class Transportadoras : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void uxFiltro_TextChanged(object sender, EventArgs e)
        {
            aplicaFiltro();
            uxTablatransportadoras.DataBind();
        }

        private void aplicaFiltro()
        {
            sqltransportadoras.FilterParameters.Clear();
            if (uxFiltro.Text.Length == 0)
            {
                sqltransportadoras.FilterExpression = null;
            }
            else
            {
                ControlParameter cp = new ControlParameter();
                cp = new ControlParameter();
                cp.ControlID = uxFiltro.ID.ToString();
                cp.PropertyName = "Text";
                cp.Name = "filtrochapa";
                cp.Type = TypeCode.String;
                cp.ConvertEmptyStringToNull = false;

                sqltransportadoras.FilterExpression = "nombre like '%{0}%'";
                sqltransportadoras.FilterParameters.Add(cp);
            }
        }

        protected void uxFiltroMarca_TextChanged(object sender, EventArgs e)
        {
            aplicaFiltro();
            uxTablatransportadoras.DataBind();

        }

        protected void uxTablatransportadoras_RowEditing(object sender, GridViewEditEventArgs e)
        {
            uxTablatransportadoras.EditIndex = e.NewEditIndex;
            aplicaFiltro();
            uxTablatransportadoras.DataBind();
        }

        protected void uxTablatransportadoras_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            finalizaEditDeleteCancel();
        }

        protected void uxTablatransportadoras_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            finalizaEditDeleteCancel();
        }

        protected void uxInsertNew_Click(object sender, EventArgs e)
        {
            sqltransportadoras.Insert();
            finalizaEditDeleteCancel();
        }

        protected void uxTablatransportadoras_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (uxTablatransportadoras.EditIndex != -1)
            {
                if (e.Row.RowIndex == uxTablatransportadoras.EditIndex)
                {
                    ImageButton myBtn = (ImageButton)e.Row.FindControl("btnDelete");
                    if (myBtn != null)
                    {
                        myBtn.Enabled = false;
                        myBtn.Visible = false;
                    }
                }

            }

        }

        protected void uxTablatransportadoras_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            finalizaEditDeleteCancel();
        }

        private void finalizaEditDeleteCancel()
        {
            uxTablatransportadoras.EditIndex = -1;
            aplicaFiltro();
            uxTablatransportadoras.DataBind();
        }
    }
}